const http = require('http');
const fs = require('fs');
const server = http.createServer(function (request, response) {
  const {url, method, headers} = request;
  if (url === '/' && method === 'GET') {
    fs.readFile('index.html', (err, data) => {
      response.statusCode = 200;
      response.setHeader("Content-Type", "text/html");
      response.end(data);
    });
  } else if (url === '/users' && method === 'GET') {
    response.writeHead(200, {'Content-Type': 'application/json'});
    response.end(JSON.stringify({
      user: '老王',
    }));
  } else if (method === 'GET' && headers.accept.indexOf('image/*') !== -1) {
    // 流
    fs.createReadStream('.' + url).pipe(response);
  }
  
}).listen(3000);

console.log('Server running at http://127.0.0.1:3000/');